import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/services/publish.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

const double kBoxSize = 50.0;

/// 浮动的内容发布进程
class FloatingPublishProcess extends GetView<PublishService> {
  const FloatingPublishProcess({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PublishService>(builder: (_) {
      return AnimatedScale(
        scale: _.show ? 1.0 : 0.0,
        duration: const Duration(milliseconds: 300),
        child: _buildMain(),
      );
      // return _.show ? _buildMain() : const SizedBox.shrink();
    });
  }

  /// 正文
  Widget _buildMain() {
    return _buildCircularBox(
      children: [
        _buildCircularBg(),
        _buildBlurText(),
        _buildCircularProgress(),
      ],
    );
  }

  /// 圆形进度条
  Widget _buildCircularProgress() {
    return const CircularProgressIndicator(
      backgroundColor: Colors.white,
      valueColor: AlwaysStoppedAnimation(AppColors.danger),
      strokeWidth: 3,
    );
  }

  /// 圆形进度文字
  Widget _buildBlurText() {
    return BackdropFilter(
      filter: ImageFilter.blur(
        sigmaX: 2,
        sigmaY: 2,
      ),
      child: Container(
        alignment: Alignment.center,
        child: Obx(
          () => Text(
            '${controller.progress}%',
            style: TextStyle(
              fontSize: 14,
              color: controller.images.isEmpty && controller.video == null
                  ? Colors.black87
                  : Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  /// 圆形背景
  Widget _buildCircularBg() {
    if (controller.images.isNotEmpty) {
      return AssetEntityImage(
        controller.images[0],
        isOriginal: false,
        fit: BoxFit.cover,
      );
    } else if (controller.video != null) {
      return AssetEntityImage(
        controller.video!,
        isOriginal: false,
        fit: BoxFit.cover,
      );
    } else {
      return const SizedBox.shrink();
    }
  }

  /// 圆形面板
  Widget _buildCircularBox({required List<Widget> children}) {
    return Container(
      decoration: BoxDecoration(
        color: AppColors.nav,
        borderRadius: BorderRadius.circular(60),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 10, // changes position of shadow
          ),
        ],
      ),
      clipBehavior: Clip.hardEdge,
      width: kBoxSize,
      height: kBoxSize,
      child: Stack(
        fit: StackFit.expand,
        children: children,
      ),
    );
  }
}
